<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>提醒功能信息列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctx}/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="${ctx}/systemStatic/style/admin.css">
    <link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>
    <link rel="stylesheet" href="${ctx}/css/public.css">
    <script type="text/javascript">
        var __ctx = "${ctx}";
    </script>
</head>
<body>

<div style="margin: 10px">
    <table id="sysPopupRemindTable" lay-filter="sysPopupRemindTable"></table>
</div>
<!-- 列表头部工具栏 -->
<script type="text/html" id="sysPopupRemindHeaderToolsBar">
    <div class="ccpui-enterSel">
        <div class="layui-btn-container">
            <a class="layui-btn layui-btn-sm" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                <span>新增</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="query">
                <i class="layui-icon layui-icon-search"></i>
                <span>查询</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="clears">
                <i class="layui-icon layui-icon-refresh"></i>
                <span>重置</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="timingPopup">
                <i class="layui-icon layui-icon-layer"></i>
                <span>演示</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="start">
                <i class="layui-icon layui-icon-play"></i>
                <span>启动所选</span>
            </a>
            <a class="layui-btn layui-btn-sm" lay-event="stop">
                <i class="layui-icon layui-icon-pause"></i>
                <span>停止所选</span>
            </a>
        </div>
        <form class="layui-form" action="" lay-filter="sysPopupRemindQueryForm" style="margin-right: 30px;">
            <div class="layui-row" style="margin-top: 10px;">
                <div class="selch table-top-filter">
                    <div class="table-filter-alone">
                        <label class="layui-form-label">标题</label>
                        <input type="text" class="layui-input filter-value" name="Q_subject_SL" placeholder="标题"/>
                    </div>
                    <div class="table-filter-alone">
                        <label class="layui-form-label">数据源别名</label>
                        <input type="text" class="layui-input filter-value" name="Q_dsalias_SL" placeholder="数据源别名"/>
                    </div>
                    <div class="table-filter-alone">
                        <label class="layui-form-label">是否启用</label>
                        <!--<input type="text" class="layui-input filter-value" name="Q_enabled_N" placeholder="是否启用"/>-->
                        <select class="filter-value" name="Q_isEnabled_N">
                            <option value="">-----请选择----</option>
                            <option value="0">禁用</option>
                            <option value="1">启用</option>
                        </select>
                    </div>
                    <div class="table-filter-alone">
                        <label class="layui-form-label">开始时间</label>
                        <input type="text" class="layui-input start_time filter-value" name="Q_beginCreatetime_DL" placeholder="开始时间"/>
                    </div>
                    <div class="table-filter-alone">
                        <label class="layui-form-label">结束时间</label>
                        <input type="text" class="layui-input end_time filter-value" name="Q_endCreatetime_DG" placeholder="结束时间"/>
                    </div>
                </div>
            </div>
        </form>
    </div>

</script>
<!-- 右侧工具栏 -->
<script type="text/html" id="sysPopupRemindRightToolsBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs" lay-event="selectAuthorizeUser">授权</a>
</script>
<script src="${ctx}/public/component.js" type="text/javascript"></script>
<script src="${ctx}/layui/layui.js"></script>
<script>
    //一般直接写在一个js文件中
    layui.config({
        base: serverPath.systemPath + staticPath.systemPath //静态资源所在路径
    }).extend({
        index: 'index' //主入口模块
    }).use(['index','table', 'setter', 'form', 'common','totalFilter','commons','jquery','admin','layer'], function () {
        var table = layui.table;
        var setter = layui.setter;
        var form = layui.form;
        var common = layui.common;
        var commons = layui.commons;
        var admin = layui.admin;
        var $ = layui.jquery;
        var totalFilter = layui.totalFilter;
        var layer = layui.layer;
        common.tableSort('sysPopupRemindTable');

        var tableH=commons.tabHeight();

        layui.authorizeUserTableData = [];

        //第一个实例
        table.render({
            elem: '#sysPopupRemindTable'
            ,height:tableH
            ,url: serverPath.basePath + '/system/sysPopupRemind/list'
            ,page: true //开启分页
            ,defaultToolbar: ['filter', 'print']
            ,toolbar: '#sysPopupRemindHeaderToolsBar'
            ,method: 'post'
            ,autoSort: false //是否排序
            ,cols: [[ //表头
                {type: 'checkbox', fixed: 'left'}
                ,{field: 'sn', type: 'numbers', title: '序号', fixed: 'left'}
                ,{field: 'id', title: '主键',hide:true,sort:true, sortname: 'id'}
                ,{field: 'subject', title: '标题',sort:true, sortname: 'subject'}
                ,{field: 'dsalias', title: '数据源别名',sort:true, sortname: 'dsalias'}
                ,{field: 'sn', title: '排序',sort:true, sortname: 'sn'}
                ,{field: 'isEnabled', title: '是否启用',sort:true, sortname: 'enabled',templet:function (d) {
                    if(d.isEnabled == 0){
                        return "禁用";
                    }else{
                        return "启用";
                    }
                }}
                //如需转换为:yyyy-MM-dd HH:mm:ss: return commons.timeData(d.createtime,"1");
                //如需转换为HH:mm:ss: return commons.timeData(d.createtime,"2");
                // 当前转换后的格式为yyyy-MM-dd: return commons.timeData(d.createtime);
                ,{field: 'createtime', title: '创建时间',sort:true, sortname: 'createtime',templet: function (d) {
                    return commons.timeData(d.createtime);
                }}
                ,{
                    fixed: 'right',
                    align: 'center',
                    title: '管理',
                    toolbar: '#sysPopupRemindRightToolsBar'
                }
            ]],done:function () {
                common.tableSelectCon('.ccpui-enterSel .layui-form input','a[lay-event=query]','.ccpui-enterSel');
            }
        });

        totalFilter.init('sysPopupRemindTable');
        commons.reflashDate({startName:'.start_time',endName:'.end_time'});

        function reflashQuery(isClear) {
            common.tableConditionRender("sysPopupRemindTable","sysPopupRemindQueryForm",{where:{}},isClear);
        }

        table.on('toolbar(sysPopupRemindTable)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            var _this = this;
            switch (obj.event) {
                case 'add':
                    $(this).attr("href", serverPath.systemPath+setter.sysLinkPath +"sysPopupRemind/view/add");
                    break;
                case "query":
                    reflashQuery(false);
                    commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                    break;
                case "clears":
                    reflashQuery(true);
                    commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                    break;
                case "start":
                    var array = "";
                    for (var i = 0; i < data.length; i++) {
                        array+=data[i].id+",";
                    }
                    startRemind(data,array);
                    break;
                case "stop":
                    var array = "";
                    for (var i = 0; i < data.length; i++) {
                        array+=data[i].id+",";
                    }
                    stopRemind(data,array);
                    break;
                case "timingPopup":
                    admin.events.timingPopup("",function(){
                        layer.msg('弹出列表为空（sql结果为0或者不启动都不算上列表）');
                    })
            };
        });

        table.on('tool(sysPopupRemindTable)', function(obj) {
            var data = obj.data, //获得当前行数据
                layEvent = obj.event; //获得 lay-event 对应的值
            switch(layEvent){
                case "edit":
                    $(this).attr("href", serverPath.systemPath+setter.sysLinkPath +"sysPopupRemind/view/add?id="+data.id);
                    break;
                case "del":
                    delFun(data,data.id);
                    break;
                case "selectAuthorizeUser":
                    $.ajax({
                        url:serverPath.systemPath + setter.sysLinkPath + 'sysPopupRemind/getPopupRemindRights',
                        type:'get',
                        dataType:'json',
                        contentType: 'application/json',
                        data:{id:data.id},
                        success : function (data) {
                            var nowData = data.data;
                            var userData = {rightType: 'user', rightTypeName: '用户授权', ownerList: []};
                            var roleData = {rightType: 'role', rightTypeName: '角色授权', ownerList: []};
                            var orgData = {rightType: 'org', rightTypeName: '组织授权', ownerList: []};
                            var allowAllUserData = {rightType:'all', rightTypeName: '所有用户', ownerList:[{ownerId: '0',ownerName: 'all'}]};
                            layui.authorizeUserTableData=[];
                            if(nowData != undefined){
                                for(var i=0;i<nowData.length;i++){
                                    switch (nowData[i].rightType){
                                        case 'user':
                                            userData.ownerList.push({ownerId: nowData[i].ownerId,ownerName: nowData[i].ownerName});
                                            if(!userData){
                                                userData = {rightType: 'user', rightTypeName: '用户授权', ownerList: []};
                                            }
                                            layui.authorizeUserTableData.push(userData);
                                            break;
                                        case 'role':
                                            roleData.ownerList.push({ownerId: nowData[i].ownerId,ownerName: nowData[i].ownerName});
                                            if(!roleData){
                                                roleData = {rightType: 'role', rightTypeName: '角色授权', ownerList: []};
                                            }
                                            layui.authorizeUserTableData.push(roleData);
                                            break;
                                        case 'org':
                                            orgData.ownerList.push({ownerId: nowData[i].ownerId,ownerName: nowData[i].ownerName});
                                            if(!orgData){
                                                orgData = {rightType: 'org', rightTypeName: '组织授权', ownerList: []};
                                            }
                                            layui.authorizeUserTableData.push(orgData);
                                            break;
                                        case 'all':
                                            layui.authorizeUserTableData.push(allowAllUserData);
                                            break;
                                    }
                                }
                            }
                            renderAuthorizeUserTable();
                        }
                    })
                    var selectUserDialogWin = null;
                    layer.open({
                        type: 2,
                        title: ['授权'],
                        area: ['500px', '600px'],
                        content: serverPath.basePath + setter.sysLinkPath +"sysPopupRemind/view/authorization?id="+data.id+"&objType=popupMsg",
                        success: function(layero, index){
                            selectUserDialogWin = window[layero.find('iframe')[0]['name']];
                            selectUserDialogWin.layui.defaultAuthorizeUserTableData = layui.authorizeUserTableData;
                        },
                        yes: function(index,layero){
                            if(selectUserDialogWin.layui.allowAllUsersFlag){
                                layui.authorizeUserTableData = [{rightType:'all', rightTypeName: '所有用户', ownerList:[{ownerId: '0',ownerName: 'all'}]}];
                            }else{
                                setAuthorizeUserTableData(selectUserDialogWin.layui.selectAuthorizeUserTableData);
                            }
                            authorizeUserTableObjIn.reload({data: layui.authorizeUserTableData});
                            layer.close(index);
                        },
                        shadeClose: false
                    })
                    break;
            }
        });

        // 赋值layui.authorizeUserTableData
        function setAuthorizeUserTableData(data){
            layui.authorizeUserTableData = [];
            layui.each(data,function(index,item){
                if(item.ownerList.length > 0){
                    layui.authorizeUserTableData.push(item);
                }
            });
        }

        // 渲染授权人员表格函数
        function renderAuthorizeUserTable(){
            authorizeUserTableObjIn = table.render({
                elem: '#authorizeUserTable',
                cols: [[
                    {field:'rightTypeName', width:160, title: '权限分类'}
                    ,{field:'ownerList', title: '授权给', templet: function(d){
                        var ownerList = d.ownerList;
                        var html = '';
                        layui.each(ownerList,function(index,item){
                            if(d.rightType === 'all'){
                                html += '<div>允许所有人访问</div>';
                            }
                            else if(d.rightType === 'user'){
                                html += '<a href="javascript:void(0);"  style="color: blue;margin-right: 5px;" onclick=onAuthUserClick("'+ item.ownerId +'")>'+ item.ownerName +'</a>';
                            }else{
                                html += '<span  style="margin-right: 5px;">'+ item.ownerName +'</span>';
                            }
                        });
                        return html;
                    }
                    }
                ]],
                data: layui.authorizeUserTableData,
                text: {
                    none: '没有授权的人员'
                }
            });
        }

        // 重置授权人员表格
        $("#resetAuthorizeUserTable").click(function(){
            layer.confirm("确认重置授权对象内容?",{icon:3},function(index){
                layui.allowAllUsersFlag = false;
                layui.authorizeUserTableData = [];
                authorizeUserTableObjIn.reload({data: []});
                layer.close(index);
            });
        });

        function delFun(data,id) {
            if (data.length === 0) {
                layer.msg('请先选择一行数据!');
            } else {
                layer.confirm('确定删除当前数据吗?', function (index) {
                    layer.close(index);
                    $.ajax({
                        url: serverPath.basePath + "/system/sysPopupRemind/del",
                        type: "post",
                        data: {id:id},
                        dataType: 'json',
                        success: function (data) {
                            layer.alert(data.msg);
                            //执行重载
                            reflashQuery(true);
                            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert(data.msg);
                            //执行重载
                            reflashQuery(true);
                            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                        }
                    })
                })
            }
        }

        /**
         * 启动当前停止状态的提醒数据
         * */
        function startRemind(data,id) {
            if(data.length === 0){
                layer.msg('请先选择一行数据!');
            }else{
                layer.confirm('确认启动当前数据吗?',function (index) {
                    layer.close(index);
                    $.ajax({
                        url: serverPath.systemPath + setter.sysLinkPath + "sysPopupRemind/startRemind",
                        type: "post",
                        data: {id:id},
                        dataType: 'json',
                        success: function (data) {
                            layer.alert(data.msg);
                            //执行重载
                            reflashQuery(true);
                            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert(data.msg);
                            //执行重载
                            reflashQuery(true);
                            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                        }
                    })
                })
            }
        }

        /**
         * 停止当前已启动的提醒数据
         * */
        function stopRemind(data,id) {
            if(data.length === 0){
                layer.msg('请先选择一行数据!');
            }else{
                layer.confirm('确认停止当前数据吗?',function (index) {
                    layer.close(index);
                    $.ajax({
                        url: serverPath.systemPath + setter.sysLinkPath + "sysPopupRemind/stopRemind",
                        type: "post",
                        data: {id:id},
                        dataType: 'json',
                        success: function (data) {
                            layer.alert(data.msg);
                            //执行重载
                            reflashQuery(true);
                            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            layer.alert(data.msg);
                            //执行重载
                            reflashQuery(true);
                            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
                        }
                    })
                })
            }
        }

        window.Initialization = function () {
            commons.reflashDate({startName:'.start_time',endName:'.end_time'});
        };
        /*刷新时间控件*/
        commons.reflashDate({startName:'.start_time',endName:'.end_time'});
        common.tableSort("sysPopupRemindTable",{show:window.Initialization});
    });
</script>
</body>
</html>
<link rel="stylesheet" href="${ctx}/css/font/iconfontie.css" media="all"/>
<link rel="stylesheet" href="${ctx}/iconfont/iconfont.css"/>